<?php
namespace Company\Controller;
use Think\Controller;
class SystemController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	//批量删除
	public function delAll_baseconfig(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	//密码修改
	public function editpwd(){
		if(IS_POST){
			$type=session('company.type');
			$username=session('company.username');
			$salt=session('company.salt');
			$pwd=I('pwd');
			$pwd_c=I('pwd_c');
			if($type==1){
				$m=M('User');
			}else{
				$m=M('Role', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}
			if($pwd==$pwd_c){
				$data['password']=md5($pwd.$salt);
				$res=$m->where(array('username'=>$username))->save($data);
				if($res!==false){
					$this->success('修改成功！');
				}else{
					$this->error('修改失败！');
				}
			}else{
				$this->error('两次密码输入不一致！');
			}
		}else{
			// dump(session());
			$this->display();
		}
	}
    public function baseconfig(){
        $m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['value'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_configs(){
			$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['add_time']=time();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_config(){
		if(IS_POST){
			$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_config(){
		$id=trim(I('id'));
		$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_config(){
		$id=trim(I('id'));
		$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	public function interfaces_del($id){
		$m=M('DistributionInterfaces');
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$m->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
	//接口列表
	public function interfaces(){
		$m = M('DistributionInterfaces');
        if(I("get.sort")!=''){
            $order="m.".I("get.sort")." ".I("get.dec");
        }else{
            $order='m.id desc';
        }
        $list = $m->alias('m')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->display();
    }
	public function interfaces_add(){
        $this->display();
    }
	public function interfaces_addD($name,$username,$appid,$app_secret){
		// $param=I('param.','','trim');
		// dump($param);
		// die;
        if($name==''){
            alert(0,'姓名不能为空');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($appid==''){
            alert(0,'APPID不能为空');
        }
        if($app_secret==''){
            alert(0,'APP_SECRET不能为空');
        }
		$m = M('DistributionInterfaces');
        $count=$m->where("username='".$username."'")->count();
        if($count>0){
            alert(0,'用户名已经存在');
        }
		$count=$m->where("appid='".$appid."'")->count();
        if($count>0){
            alert(0,'appid已经存在');
        }
		$count=$m->where("app_secret='".$app_secret."'")->count();
        if($count>0){
            alert(0,'app_secret已经存在');
        }
        $data['name']=$name;
        $data['username']=$username;
		$data['appid']=$appid;
		$data['app_secret']=$app_secret;
		$data['status']=1;
		$data['erp_code']=session('company.id');
		
        $result=$m->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
	public function interfaces_edit($id){
		$m = M('DistributionInterfaces');
        if($id==''){
            echo "错误";
        }
        $this->data=$m->find($id);
        $this->display();
    }
	public function interfaces_editD($id,$name,$appid,$app_secret){
		$m = M('DistributionInterfaces');
        if($name==''){
            alert(0,'姓名不能为空');
        }
        if($appid==''){
            alert(0,'appid不能为空');
        }
		if($app_secret==''){
            alert(0,'app_secret不能为空');
        }
        $data['name']=$name;
        $data['appid']=$appid;
		$data['app_secret']=$app_secret;
		
        $result=$m->where("id='".$id."'")->save($data);
        if($result!==false){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
}